package com.code.petapp.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.code.petapp.R;
import com.code.petapp.bean.AnimalBean;

import java.util.List;

public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder> {
    private Context context;
    private List<AnimalBean> list;
    private View inflater;
    private OnItemClickListener mListener;

    public HomeAdapter(Context context, List<AnimalBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        inflater = LayoutInflater.from(context).inflate(R.layout.item_home, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(inflater);
        return myViewHolder;
    }
    public interface OnItemClickListener {
        void onClick(View parent, int position);
    }
    // 设置点击事件
    public void setOnItemClickListener(OnItemClickListener l) {
        this.mListener = l;
    }
    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        AnimalBean animalBean=list.get(position);
        Log.d("ZZ","animalBean:"+animalBean);
        holder.txName.setText(animalBean.getName());
        // 使用 Glide 加载网络图片
        Glide.with(context)
                .load(animalBean.getImg())
                .into(holder.imgPet);
        holder.txAge.setText(Integer.toString(animalBean.getAge()));
        holder.txStatus.setText(animalBean.getStatus());
        if(animalBean.getStatus().equals("待领养")){
            holder.imgAdopt.setImageResource(R.drawable.icons_can_adopt);
            holder.imgAdopt.setEnabled(true);
        }else{
            holder.imgAdopt.setImageResource(R.drawable.icons_cant_adopt);
            holder.imgAdopt.setEnabled(false);
        }
        if(animalBean.getSex().equals("公")){
            holder.sexIcon.setImageResource(R.drawable.icons_man);
        }else{
            holder.sexIcon.setImageResource(R.drawable.icons_wom);
        }
        if(null != mListener) {
            holder.imgAdopt.setOnClickListener(v -> mListener.onClick(holder.imgAdopt, position));
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView txName;
        ImageView imgPet;
        TextView txAge;
        TextView txStatus;
        ImageView imgAdopt;
        ImageView sexIcon;


        public MyViewHolder(View itemView) {//这个view参数就是recyclerview子项的最外层布局
            super(itemView);
            //可以通过findViewById方法获取布局中的TextView
            txName =itemView.findViewById(R.id.txName);
            imgPet = itemView.findViewById(R.id.imgPet);
            sexIcon=itemView.findViewById(R.id.sexIcon);
            txAge =itemView.findViewById(R.id.txAge);
            txStatus = itemView.findViewById(R.id.txStatus);
            imgAdopt = itemView.findViewById(R.id.imgAdopt);
        }
    }
}
